from django import forms
from web.forms.project import BootstragForm
from scripts.init_price_police import S28


# class proBlem(BootstragForm, forms.Form):
#
#     def __init__(self, requested, *args, **kwargs):
#         super().__init__(*args, **kwargs)
#         self.prople = [('','没有选中任何人')]
#         self.request_project_id = requested
#         self.create_ids = S28().py_searche('select creator_id from project where id="{}"'.format(self.request_project_id)) # int类型
#         print(self.create_ids)
#         self.my_self = S28().py_searche('select concat(id),user from user_information where id="{}"'.format(self.create_ids[0][0]))
#
#         self.prople.append(self.my_self[0])
#
#         self.invitation = S28().py_searche('select * from invitation where project_id="{}"'.format(self.request_project_id))
#         if len(self.invitation) != 0:
#             for i in self.invitation:
#                 ret = S28().py_searche('select id,user from user_information where id="{}"'.format(i[2]))
#                 self.prople.append(ret[0])
#         print(self.prople, '_______________________我的大宝贝————————-------------------------')
#         father_problem = [('0','没有任何的父问题')]
#         problems_num = S28().py_searche('select id,the_title from problem')
#         if len(problems_num) !=0:
#             for i in problems_num:
#                 father_problem.append(i)
#
#         self.fields['assign_user_information'].choices = self.prople
#         self.fields['follow_user_information'].choices = self.prople
#         self.fields['parent_problem'].choices = father_problem
#         print(self.fields['assign_user_information'].choices)
#
#
#
#
#     the_title = forms.CharField(max_length=64, label='主题')
#     the_content = forms.CharField(max_length=65500, label='内容', widget=forms.Textarea)
#     problem_type = forms.ChoiceField(choices=[('BUG', 'BUG'),('功能', '功能'),('任务', '任务')], label='类型',widget=forms.Select(attrs={'class':'selectpicker'}))
#     modular = forms.CharField(max_length='64', label='模块')
#     statused = forms.ChoiceField(choices=(('新建','新建'),('处理中','处理中'),('已解决','已解决'),('已忽略','已忽略'),('待反馈','待反馈'),('已关闭','已关闭'),('重新打开','重新打开')), label='状态',
#                                  widget=forms.Select(attrs={'class':'selectpicker'}))
#     assign_user_information = forms.ChoiceField(label='派遣', widget=forms.Select(),required=False)
#     follow_user_information = forms.ChoiceField(label='跟踪',widget=forms.Select(), required=False)
#     start_time = forms.DateTimeField(label='开始时间')
#     end_time = forms.DateTimeField(label='结束时间')
#     pattern = forms.ChoiceField(label='是否公开',choices=(('false', '不公开'),('true', '公开')),widget=forms.Select(attrs={'class':'selectpicker'}))
#     parent_problem =forms.ChoiceField(label='父问题',widget=forms.Select(attrs={'class':'selectpicker','data-live-search':'true'}),required=False)


    # 'data-live-search':'true'  'class':'selectpicker'

class ShowProblem(BootstragForm,forms.Form):

    def __init__(self, requested, val=None, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.prople = [('','没有选中任何人')]   # 最后循环生产的用户列表
        father_problem = [('0', '没有任何的父问题')]  # 最后生成的父问题的列表
        self.request_project_id = requested  # 把这个项目的id传进来
        # 定义一个列表  查出来创建者的id 把这个id和他的用户名写入
        self.create_ids = S28().py_searche('select creator_id from project where id="{}"'.format(self.request_project_id)) # int类型

        self.my_self = S28().py_searche('select concat(id),user from user_information where id="{}"'.format(self.create_ids[0][0]))
        self.prople.append(self.my_self[0])

        # 接下来就是查询当前项目的关注者的id和名字  并且添加到self.prople 列表里面去
        self.invitation = S28().py_searche('select * from invitation where project_id="{}"'.format(self.request_project_id))
        if len(self.invitation) != 0:
            for i in self.invitation:
                ret = S28().py_searche('select id,user from user_information where id="{}"'.format(i[2]))
                self.prople.append(ret[0])

        # 接下来就父问题的添加  循环把所有的问题都放在一个列表里面去
        problems_num = S28().py_searche('select id,the_title from problem')
        if len(problems_num) !=0:
            for i in problems_num:
                father_problem.append(i)
        # 设置他们的choices
        self.fields['assign_user_information'].choices = self.prople   # 派遣者
        self.fields['follow_user_information'].choices = self.prople  # 跟踪者
        self.fields['parent_problem'].choices = father_problem  # 父问题

        self.vals = val
        if self.vals == None:
            pass
        else:
            numsed = 0
            for name,field in self.fields.items():
                self.fields[name].initial = self.vals[numsed]
                numsed +=1

    the_title = forms.CharField(max_length=64, label='主题')
    the_content = forms.CharField(max_length=65500, label='内容', widget=forms.Textarea)
    problem_type = forms.ChoiceField(choices=[('BUG', 'BUG'),('function', '功能'),('task', '任务')], label='类型',widget=forms.Select(attrs={'class':'selectpicker'}),initial='BUG')
    modular = forms.CharField(max_length='64', label='模块')
    statused = forms.ChoiceField(choices=(('new','新建'),('process','处理中'),('solve','已解决'),('ignore','已忽略'),('feedback','待反馈'),('close','已关闭'),('reopen','重新打开')), label='状态',
                                 widget=forms.Select(attrs={'class':'selectpicker'}))
    assign_user_information = forms.ChoiceField(label='派遣', widget=forms.Select(),required=True)
    follow_user_information = forms.ChoiceField(label='跟踪',widget=forms.Select(), required=True)
    start_time = forms.DateTimeField(label='开始时间')
    end_time = forms.DateTimeField(label='结束时间')
    pattern = forms.ChoiceField(label='是否公开',choices=(('false', '不公开'),('true', '公开')),widget=forms.Select(attrs={'class':'selectpicker'}))
    parent_problem =forms.ChoiceField(label='父问题',widget=forms.Select(attrs={'class':'selectpicker','data-live-search':'true'}),required=False)



class issuesRecord(forms.Form):
    content = forms.CharField()
    father_id = forms.CharField()
